import {
  workerList,
  getImage
} from '../../api/worker'
const app = getApp().globalData
Page({
  data: {
    imageUrl: '',
    statusBarHeight: wx.getSystemInfoSync()['statusBarHeight'],
    //statusBarHeight:0,
    shownavi: false,
    scTopDelt: 0,
    formMark: true,
    list: [],
    originData: [],
    showSearch: false, //  是否显示搜索框
    searchKey: '' // 关键字
  },

  onLoad: function (e) {
    //this.reloadData();
  },
  toggleSearch() {
    this.setData({ showSearch: true, searchKey: '' })
  },
  onSearch(e) {
    this.setData({ searchKey: e.detail })
    this.setData({
      list: this.data.originData.filter((i) => i.nickName.includes(e.detail))
    })
  },
  onCancel() {
    this.setData({ 
      searchKey: '', showSearch: false,
      list: this.data.originData
   })
  },
  onShow() {
    if (app.isReload) {
      app.isReload = false
      this.data.finished = false
      // this.reloadData()

    }
    this.reloadData();
  },
  openlocation() {
    wx.openLocation({
      latitude: 36.92021,
      longitude: 121.5402,
      scale: 5,
      name: '黄南政务',
      address: '地址：乳山358路,乳山212路,乳山211路'
    })
  },
  gotab() {
    // wx.navigateTo({
    //   url: '/pages/index/info/index?worker=1'
    // })
    wx.switchTab({
      url: '/pages/index/hotList/index'
    })
  },
  goFarmList(e) {
    //这里我们跳转详情界面
    var that = this
    //拿到点击的index下标
    var index = e.currentTarget.dataset.index
    //将对象转为string
    //console.log(e)
    // console.log(that.data.list[index])
    var queryBean = JSON.stringify(that.data.list[index])
    wx.navigateTo({
      url: '/pages/index/info/index?worker=' + queryBean,
    })
  },
  gopage(e) {
    const {
      url
    } = e.currentTarget.dataset
    wx.navigateTo({
      url
    })
  },
  onPullDownRefresh: function () {
    this.reloadData()
  },
  arrjoin(arr) {
    return arr.join('|')
  },
  reloadData() {
    getImage().then((res) => {
      this.setData({
        //formMark: true,
        imageUrl: res.datas
      })
    }),
    this.data.page = 1
    this.data.finished = false
    this.getList()
  },
  onPageScroll(e) {
    const tp = e.scrollTop
    if (Math.abs(tp - this.data.scTopDelt) < 40) {
      return
    }
    this.setData({
      shownavi: tp > 70,
      scTopDelt: tp
    })
  },
  onReachBottom() {
    if (this.data.finished) {
      return
    }
    this.data.page++
    this.getList()
  },
  getList() {
    const p = {
      page: this.data.page,
      k: ''
    }
    workerList(p).then((res) => {
      this.setData({
        formMark: true,
        finished: true,
        list: this.data.page > 1 ? this.data.list.concat(res.datas) : res.datas
      })
      this.data.originData = JSON.parse(JSON.stringify(this.data.list))
      // if (this.data.list.lenght >= res.total) {
      //   this.data.finished = true
      // }
    })
  }
})